package com.jkt.rms.dataservice.impl;



import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.jkt.rms.business.AppRole;
import com.jkt.rms.business.Users;
import com.jkt.rms.commons.utils.RMSException;
import com.jkt.rms.commons.utils.SecurityHelper;
import com.jkt.rms.dataservice.AbstractDataService;
import com.jkt.rms.dataservice.UserDataService;

public class UserDataServiceImpl extends AbstractDataService<Users> implements
		UserDataService {
 
	public AppRole validateUser(String empCode, String password) {
		String hql = "from com.jkt.rms.business.AppRole u where u.loginID = :empCode and u.password = :password";
		Query hqlQuery = getSession().createQuery(hql);
		hqlQuery.setParameter("empCode", empCode, Hibernate.STRING);
		hqlQuery.setParameter("password", SecurityHelper.encrypt(password), Hibernate.STRING);
		AppRole tmsUser = (AppRole) hqlQuery.uniqueResult();
		getSession().close();
		return tmsUser;
	}

	@Override
	protected Class<Users> getReferenceClass() {
		return Users.class;
	}

	public void updateUser(Users appRoleUserData) throws RMSException{
			saveOrUpdate(appRoleUserData);
	}

	public Users getUserData(String userName) {
		DetachedCriteria criteria = DetachedCriteria.forClass(getReferenceClass());
		 criteria.add(Restrictions.eq(Users.PROP_USERNAME, userName));
		List<Users> users = findByCriteria(criteria);
		if(users != null && users.size() > 0 ) {
			return users.get(0);
		}else {
			return null;
		}
	}
	
	public Users saveUsers(Users user) throws RMSException {
		return save(user);
		
		
	}
	@Override
	public List<Users> findAll() {
		return super.findAll();
	}

	public List<Users> findByCriteria(DetachedCriteria crit) { 
		return super.findByCriteria(crit);
	}
	
	@Override
	public void updateUserData(Users user) throws RMSException{
		super.updateObj(user);	
	}
	
}
